home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / lang / HeliOS3.lha / helios_demo_disk3 / source / Demo3_SimpleSprite.src < prev    next >
Encoding:
Text File  |  1995-11-11  |  15.4 KB  |  585 lines

  1.  
  2.   \ ***********************************************************
  3.   \
  4.   \                    SIMPLE SPRITE DEMO
  5.   \
  6.   \ ***********************************************************
  7.   \
  8.   \ This code demonstrates how to create a simple HeliOS single
  9.   \ sprite (a BOB to be precise) and move it around.
  10.   \
  11.   \ This code builds upon the following Demos:
  12.   \
  13.   \ Demo1_SinglePF.src
  14.   \ Demo2_SinglePFCopper.src
  15.   \
  16.   \ This code is then expanded in the following Demos:
  17.   \
  18.   \ Demo4_MultiSprites.src
  19.   \ Demo5_MultiSptCollide.src
  20.   \ Demo6_SimpleAnim.src
  21.   \ Demo7_MultiAnim.src
  22.   \
  23.   \ ***********************************************************
  24.  
  25.  
  26.   \ ***********************************************************
  27.   \ Re-initialise HeliOS dictionary to standard CORE vocabulary
  28.   \ ***********************************************************
  29.  
  30.   \ This should always be used at the start of any program which
  31.   \ is to be repeatedly recompiled.
  32.  
  33.   FORGET **CORE**
  34.  
  35.   \ *************************
  36.   \ Load include symbol files
  37.   \ *************************
  38.   \
  39.   \ These "include files" are pre-compiled (for speed) versions of the
  40.   \ Amiga includes and the Helios system includes.
  41.   \
  42.   \ Uncomment the lines below for standalone compilation, but otherwise
  43.   \ it is better to set these include files from the Helios Forth menus
  44.  
  45.   AMIGAINCLUDE HeliOS:HeliOS_AmigaInclude
  46.   USERINCLUDE  HeliOS:HeliOS_UserInclude
  47.  
  48.   \ ****************************************
  49.   \ Create display imagery file name strings
  50.   \ ****************************************
  51.  
  52.   \ These files are ordinary IFF's (and may be "PowerPacked" if required)
  53.   \
  54.   \ The pictures here will be loaded into each of the display BitMaps.
  55.   \
  56.  
  57.   $CONSTANTL Slice1Pic $Helios:Source/Data/Pic1$
  58.  
  59.   \ **************************************
  60.   \ Create display configuration constants
  61.   \ **************************************
  62.   \
  63.   \ Collect all "display-specific" parameters here and generate "named"
  64.   \ constants which make references easier than using numeric values.
  65.   \
  66.   \ Collecting these together here makes it easier to adjust things at any
  67.   \ time without having to search source code to replace values individually.
  68.   \
  69.  
  70.  
  71.   256                      CONSTANT DisplayHeight      \ Full PAL display
  72.   320                      CONSTANT DisplayWidth       \ Lores display
  73.   44                       CONSTANT DisplayTopLine     \ Display start
  74.  
  75.   DisplayWidth             CONSTANT Slice1Width        \ Lores width
  76.   DisplayWidth 32 +        CONSTANT Slice1RasterWidth  \ Raster=SWidth+32
  77.   DisplayHeight            CONSTANT Slice1Height       \ Slice height
  78.   DisplayHeight 32 +       CONSTANT Slice1RasterHeight \ Slice Raster=DHgt+32
  79.   0                        CONSTANT Slice1Mode         \ Lores
  80.   3                        CONSTANT Slice1Planes       \ Slice bitplanes
  81.  
  82.   \ The calculation below takes the number of bitplanes and calculates
  83.   \ how many colours this represents.
  84.   \
  85.   \ One bitplane gives two colours.
  86.   \
  87.   \ Each additional bitplane multiplies the number of colours by two.
  88.   \
  89.   \ Performing a single LSL operation on any number multipies by 2, so we
  90.   \ have a quick method of multiplying by two for our colour calculation.
  91.   \
  92.   \ In this case, we need "2 to the power 3", which is 2*2*2=8.
  93.   \
  94.   \ e.g.                              2*2*2
  95.   \                                   ^ ^ ^
  96.   \                                   Total number of planes = 3
  97.   \
  98.   \
  99.   \ So, we take the first value two
  100.   \
  101.   \ e.g.                              2*2*2
  102.   \                                   ^
  103.   \                                  Initial start value of 2
  104.   \
  105.   \ and we now need to multiply it by two "the_number_of_planes minus_one"
  106.   \ more times.
  107.   \
  108.   \ e.g.                              2*2*2
  109.   \                                     ^ ^
  110.   \                                     Total planes minus one
  111.   \
  112.   \
  113.   \ So, Number of colours = 2 operated on by LSL NumberOfPlanes-1 times.
  114.   \
  115.  
  116.   2 Slice1Planes 1- LSL    CONSTANT Slice1Colours      \ A-slice colours
  117.  
  118.   \ ***********************
  119.   \ Error handling routines
  120.   \ ***********************
  121.  
  122.   \ This error handler allows all errors to be routed via a comprehensive
  123.   \ sequential closedown routine, which is associated with the HeliOS
  124.   \ system error handler word ERROR".
  125.   \
  126.   \ When ERROR" senses an error, it prints an associated error message
  127.   \ delimited by '"' characters, and then closes everything down using the
  128.   \ routine CLOSEDOWN below which you have supplied.
  129.   \
  130.   \ This simplifies errors checks to the use of a single word, ERROR", which
  131.   \ displays a text message and closes eveything down.
  132.   \
  133.  
  134.   0 VARIABLE (CLOSEDOWN)
  135.  
  136.   : ?CLOSEDOWNERROR
  137.  
  138.   IF
  139.     CR
  140.     CR
  141.     TYPE
  142.     CR
  143.     CR
  144.     ." Press <Space> to quit!"
  145.     CR
  146.     CR
  147.     WAITSPACE
  148.     (CLOSEDOWN) @EXECUTE
  149.     QUIT
  150.   ELSE
  151.     DDROP
  152.   THEN
  153.   ;
  154.  
  155.   LATESTCFA VARIABLE ERROR1
  156.  
  157.   \ ****************************************
  158.   \ Create display pointer storage variables
  159.   \ ****************************************
  160.  
  161.   \ Here we create a set of "pointers", initially set to a "null" value.
  162.   \
  163.   \ These "pointers" are set up as "long addresses" when various components
  164.   \ of the display system are allocated and initialised.
  165.   \
  166.   \ Note that initially these are all set to zero, and we clear them back
  167.   \ to zero when we de-allocate the associated resource.
  168.   \
  169.   \ These DPOINTERs are all initially set to "null" by using '0.'.
  170.   \
  171.   \ When we allocate memory or Amiga system resources in the program at
  172.   \ run-time, these pointers are updated to contain the 32-bit address
  173.   \ of the newly allocated resource.
  174.   \
  175.   \ Subsequently the symbolic DPOINTER name can be used in your code to
  176.   \ represent the associated address.
  177.  
  178.   0. DPOINTER Display1             \ Main Display structure pointer
  179.   0. DPOINTER Slice1               \ Slice 1 Slice structure pointer
  180.  
  181.   0. DPOINTER Slice1_ColorMap      \ Slice 1 ColourMap structure pointer
  182.  
  183.   0. DPOINTER Slice1_RasInfo       \ Slice 1 RasInfo structure pointer
  184.  
  185.   0. DPOINTER Slice1_BMap          \ Slice 1 BitMap structure pointer
  186.  
  187.   0. DPOINTER Slice1_SliceControl  \ Slice 1 SliceControl structure pointer
  188.  
  189.   \ *************
  190.   \ Colour tables
  191.   \ *************
  192.  
  193.   \ Each colour entry requies 2 bytes of storage space
  194.  
  195.   CREATEL Slice1_ColorTable        \ Create longword pointer to table
  196.   Slice1Colours 2* 0 ALLOTFILL     \ Allocate Slice1 colours * 2 bytes
  197.  
  198.  
  199.   \ *************************************
  200.   \ Copper strip for graduated background
  201.   \ *************************************
  202.  
  203.   0. DVARIABLE Copper              \ 32-bit CopperList pointer store
  204.   0. DVARIABLE CopperLength        \ 32-bit CopperList length store
  205.  
  206.   : AddCopper                      \ Add custom copper list to display
  207.  
  208.   Display1                         \ We are adding CopperList to Display1
  209.   Copper D@
  210.   ADDCOPPERSTRIP
  211.   SORTSTRIPTABLE
  212.   LINKSTRIPS
  213.   DDROP
  214.   ;
  215.  
  216.   : RemCopper                      \ Remove custom copper list from display
  217.  
  218.   Display1                         \ We are removing CopperList from Display1
  219.   Copper D@
  220.   DFLAG
  221.   IF
  222.     REMCOPPERSTRIP
  223.     LINKSTRIPS
  224.     DDROP
  225.   ELSE
  226.     DDDROP
  227.   THEN
  228.   ;
  229.  
  230.   : Create_Copper
  231.  
  232.   COPPERSTART
  233.   Copper D!
  234.   0 [ DECIMAL ]  45 [ HEX ] FFFE COPPERWAIT  DROP   0100 18E COPPERMOVE  DROP
  235.   0 [ DECIMAL ]  60 [ HEX ] FFFE COPPERWAIT  DROP   0200 18E COPPERMOVE  DROP
  236.   0 [ DECIMAL ]  75 [ HEX ] FFFE COPPERWAIT  DROP   0300 18E COPPERMOVE  DROP
  237.   0 [ DECIMAL ]  90 [ HEX ] FFFE COPPERWAIT  DROP   0400 18E COPPERMOVE  DROP
  238.   0 [ DECIMAL ] 105 [ HEX ] FFFE COPPERWAIT  DROP   0500 18E COPPERMOVE  DROP
  239.   0 [ DECIMAL ] 120 [ HEX ] FFFE COPPERWAIT  DROP   0600 18E COPPERMOVE  DROP
  240.   0 [ DECIMAL ] 135 [ HEX ] FFFE COPPERWAIT  DROP   0700 18E COPPERMOVE  DROP
  241.   0 [ DECIMAL ] 150 [ HEX ] FFFE COPPERWAIT  DROP   0800 18E COPPERMOVE  DROP
  242.   0 [ DECIMAL ] 165 [ HEX ] FFFE COPPERWAIT  DROP   0900 18E COPPERMOVE  DROP
  243.   0 [ DECIMAL ] 180 [ HEX ] FFFE COPPERWAIT  DROP   0A00 18E COPPERMOVE  DROP
  244.   0 [ DECIMAL ] 195 [ HEX ] FFFE COPPERWAIT  DROP   0B00 18E COPPERMOVE  DROP
  245.   0 [ DECIMAL ] 210 [ HEX ] FFFE COPPERWAIT  DROP   0C00 18E COPPERMOVE  DROP
  246.   0 [ DECIMAL ] 225 [ HEX ] FFFE COPPERWAIT  DROP   0D00 18E COPPERMOVE  DROP
  247.   0 [ DECIMAL ] 240 [ HEX ] FFFE COPPERWAIT  DROP   0E00 18E COPPERMOVE  DROP
  248.   0 [ DECIMAL ] 255 [ HEX ] FFFE COPPERWAIT  DROP   0F00 18E COPPERMOVE  DROP
  249.   [ DECIMAL ]
  250.   COPPEREND
  251.   CopperLength D!   Copper D!
  252.   ADDCOPPER
  253.   ;
  254.  
  255.   : Free_Copper                  \ Closes down and frees copperlist memory
  256.  
  257.   RemCopper
  258.   Copper D@ FREEMEMORY
  259.   ;
  260.  
  261.   \ ***********************************
  262.   \ Create Display and Slice structures
  263.   \ ***********************************
  264.  
  265.   \ This routine simply makes blank structures, which then need to be
  266.   \ initialised later (in the CREATE_DISPLAY routine).
  267.  
  268.   : CREATE_DSLICES
  269.  
  270.   DS_SIZEOF MAKESTRUCTURE Display1 MAKEPOINTER  \ Main "Display" structure
  271.  
  272.   SL_SIZEOF MAKESTRUCTURE Slice1   MAKEPOINTER  \ Display "Slice" structure
  273.   ;
  274.  
  275.   : FREE_DSLICES
  276.  
  277.   Slice1    DDUP FREEMEMORY   CLEARPOINTER
  278.   Display1  DDUP FREEMEMORY   CLEARPOINTER
  279.   ;
  280.  
  281.   \ ******************************
  282.   \ Create RasInfo structures etc.
  283.   \ ******************************
  284.  
  285.   : CREATE_RASINFO
  286.  
  287.   \ First allocate and initialise complete RasInfo structures.
  288.   \
  289.   \ This routine automatically allocates all BitMaps etc.
  290.   \
  291.  
  292.   Slice1RasterWidth Slice1RasterHeight Slice1Planes  OPENRASINFO
  293.   DFLAG0= ERROR" Fail: RasInfo1"
  294.   Slice1_RasInfo MAKEPOINTER
  295.  
  296.   \ Set invisible area "sprite margins" for slice RasInfo
  297.  
  298.   16              Slice1_RasInfo         ri_RxOffset    INDEX!L
  299.   16              Slice1_RasInfo         ri_RyOffset    INDEX!L
  300.  
  301.   \ Store BitMap pointer - often useful for later reference
  302.  
  303.   Slice1_RasInfo  ri_BitMap INDEXD@L Slice1_BMap MAKEPOINTER
  304.   ;
  305.  
  306.   : FREE_RASINFO
  307.  
  308.   Slice1_RasInfo   DDUP CLOSERASINFO   CLEARPOINTER
  309.   ;
  310.  
  311.   \ ********************************
  312.   \ Create Display/Slice Copperlists
  313.   \ ********************************
  314.  
  315.   \ This function builds the main display copperlist by:
  316.   \
  317.   \ 1. Initialising the Slice data structure
  318.   \ 2. Calling MAKECOPSTRIP for the slice, to build a copperlist
  319.   \ 3. Calling MAKEDISPLAY to build the master Display copperlist
  320.   \
  321.  
  322.   : CREATE_DISPLAY
  323.  
  324.   \ First initialise main display structures
  325.  
  326.   Slice1                           Display1  DS_Slice     INDEXD!L
  327.  
  328.   Slice1Width                      Slice1    SL_DWidth    INDEX!L
  329.   Slice1Height                     Slice1    SL_DHeight   INDEX!L
  330.   DisplayTopLine                   Slice1    SL_DyOffset  INDEX!L
  331.   Slice1_RasInfo                   Slice1    SL_RasInfo   INDEXD!L
  332.   Slice1_ColorMap                  Slice1    SL_ColorMap  INDEXD!L
  333.   Slice1Mode                       Slice1    SL_Modes     INDEX!L
  334.  
  335.   \ Generate copper list information for each of the display slices
  336.  
  337.   Slice1 MAKECOPSTRIP
  338.   D0= ERROR" Fail: Slice1CopStrip"
  339.  
  340.   \ Make display
  341.  
  342.   Display1 MAKEDISPLAY
  343.   D0= ERROR" Fail: Display1"
  344.   ;
  345.  
  346.   : FREE_DISPLAY
  347.  
  348.   Display1                 FREEDISPLAY
  349.   Slice1                   FREECOPSTRIP
  350.   ;
  351.  
  352.   \ ********************************************************
  353.   \ Create SliceControl structures for double buffered slice
  354.   \ ********************************************************
  355.  
  356.   \ SliceControl structures are used to control any slices which perform
  357.   \ mapping or scrolling functions, or which require double or triple
  358.   \ playfield buffering.
  359.   \
  360.   \ In this case we have one slice which does not scroll, is not mapped,
  361.   \ but IS double buffered.
  362.   \
  363.  
  364.   : CREATE_SLICECONTROL
  365.  
  366.   \ Make SliceControl for double buffered bitmap display
  367.  
  368.   Slice1  0 0 MAKESLICECONTROL
  369.   DFLAG0= ERROR" Fail: SliceControl1"
  370.   Slice1_SliceControl MAKEPOINTER
  371.  
  372.   \ Install slice controls into HeliOS display control system
  373.  
  374.   Slice1_SliceControl  INSTALLSLICECONTROL
  375.   ;
  376.  
  377.   : FREE_SLICECONTROL
  378.  
  379.   CLEARSLICECONTROLS
  380.   Slice1_SliceControl  CLOSESLICECONTROL
  381.   ;
  382.  
  383.   \ These routines load an IFF picture into supplied BitMap, and correctly
  384.   \ initialises the supplied ColorTable.
  385.   \
  386.   \ The ColorTable is then used to create an initialised ColorMap structure.
  387.   \
  388.  
  389.   : CREATE_IMAGERY
  390.  
  391.   Slice1_BMap
  392.   Slice1_ColorTable
  393.   Slice1Pic
  394.   10 2 DOSLIB                        \ Call to internal HeliOS library
  395.   10 <> ERROR" Fail: Slice1Pic"
  396.  
  397.   Slice1_ColorTable  Slice1Colours MAKECOLORMAP  \ Allocate ColourMap
  398.   DFLAG0= ERROR" Fail: Slice1ColorMap"
  399.   Slice1_ColorMap MAKEPOINTER
  400.   ;
  401.  
  402.   : FREE_IMAGERY
  403.  
  404.   Slice1_ColorMap  DDUP FREECOLORMAP  CLEARPOINTER
  405.   ;
  406.  
  407.   \ ***********
  408.   \ Sprite Demo
  409.   \ ***********
  410.  
  411.   \ *********************
  412.   \ Sprite Demo constants
  413.   \ *********************
  414.  
  415.   \ Store the gun speed as a CONSTANT
  416.  
  417.   4  CONSTANT GunSpeed             \ Speed of Gun movement
  418.  
  419.   \ *************************************************
  420.   \ Create sprite demo pointers and storage variables
  421.   \ *************************************************
  422.  
  423.   0. DPOINTER GunSpriteSet         \ Gun sprite image
  424.  
  425.   \ ***************************
  426.   \ User input response routine
  427.   \ ***************************
  428.  
  429.   : MoveGun
  430.  
  431.   RAWKEY @ 78 =
  432.   JOY1LEFTRIGHT 0>
  433.   OR
  434.   IF
  435.      GunSpeed
  436.      19 320
  437.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  438.      LIMIT+!L
  439.   ELSE
  440.     RAWKEY @ 79 =
  441.     JOY1LEFTRIGHT 0<
  442.     OR
  443.     IF
  444.      GunSpeed NEGATE
  445.      19 320
  446.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  447.      LIMIT+!L
  448.     THEN
  449.   THEN
  450.   ;
  451.  
  452.   \ ********************
  453.   \ Create sprite object
  454.   \ ********************
  455.  
  456.   : CREATE_SPRITE
  457.  
  458.   \ ----------
  459.   \ Gun sprite
  460.   \ ----------
  461.  
  462.   Slice1_BMap
  463.   154 272
  464.   13 15
  465.   1
  466.   MAKESPRITESET
  467.   DFLAG0= ERROR" Fail: Gun SpriteSet"
  468.   GunSpriteSet MAKEPOINTER
  469.  
  470.   GunSpriteSet 8. D+ D@L
  471.   DDUP  SpriteCtrl_XPos D+       170 -ROT !L
  472.         SpriteCtrl_YPos D+       239 -ROT !L
  473.  
  474.   Slice1_SliceControl   GunSpriteSet    INITSPRITESET
  475.   ;
  476.  
  477.   : FREE_SPRITE
  478.  
  479.   GunSpriteSet    DDUP FREESPRITESET CLEARPOINTER
  480.   ;
  481.  
  482.   \ *********************
  483.   \ Close down everything
  484.   \ *********************
  485.  
  486.   : CLOSEDOWN
  487.  
  488.   FREE_SPRITE
  489.   FREE_COPPER
  490.   FREE_SLICECONTROL
  491.   FREE_DISPLAY
  492.   FREE_IMAGERY
  493.   FREE_RASINFO
  494.   FREE_DSLICES
  495.   RESETERROR"
  496.   ;
  497.  
  498.   LATESTCFA (CLOSEDOWN) !
  499.  
  500.   : TestDisplay          \ Start of program
  501.  
  502.   SCRCLR
  503.   CR
  504.   ."        **********************************************************"
  505.   CR 6 FPENSET
  506.   ."                           SIMPLE SPRITE DEMO"
  507.   CR 1 FPENSET
  508.   ."        **********************************************************"
  509.   CR
  510.   CR
  511.   ."        This code demonstrates how to create a simple HeliOS single"
  512.   CR
  513.   ."        sprite (a BOB to be precise) and move it around."
  514.   CR
  515.   CR
  516.   ."        This code build upon the following Demos:"
  517.   CR
  518.   CR
  519.   ."        Demo1_SinglePF.src"
  520.   CR
  521.   ."        Demo2_SinglePFCopper.src"
  522.   CR
  523.   CR
  524.   ."        This code is then expanded in the following Demos:"
  525.   CR
  526.   CR
  527.   ."        Demo4_MultiSprites.src"
  528.   CR
  529.   ."        Demo5_MultiSptCollide.src"
  530.   CR
  531.   ."        Demo6_SimpleAnim.src"
  532.   CR
  533.   ."        Demo7_MultiAnim.src"
  534.   CR
  535.   ."        **********************************************************"
  536.   CR 6 FPENSET
  537.   ."                  Press <Space> or <L-Mouse> to see Demo          "
  538.   CR 3 FPENSET
  539.   ."         Use a Joystick to move the gun turret from left to right"
  540.   CR
  541.   ."        **********************************************************"
  542.   CR
  543.  
  544.   WAITSPACE
  545.  
  546.   SCRCLR
  547.  
  548.  
  549.   ERROR1 SETERROR"       \ Redirect system errors to our routine ERROR1
  550.  
  551.   CREATE_DSLICES
  552.   CREATE_RASINFO
  553.   CREATE_IMAGERY
  554.   CREATE_DISPLAY
  555.   CREATE_SLICECONTROL
  556.   CREATE_COPPER
  557.   CREATE_SPRITE
  558.  
  559.   HeliOS_On
  560.  
  561.   GunSpriteSet 8. D+ D@L INSTALLSPRITE
  562.  
  563.   1 FrameRate !L
  564.  
  565.   Display1 SHOWDISPLAY
  566.  
  567.   BEGIN
  568.     WAITFRAME
  569.  
  570.     MoveGun
  571.  
  572.     ?TERMINAL 27 =
  573.   UNTIL
  574.  
  575.   -3 GunSpriteSet 8. D+ D@L SpriteCtrl_Flags INDEX!L
  576.  
  577.   5 DELAY
  578.  
  579.   HeliOS_Off
  580.  
  581.   CLOSEDOWN
  582.   ;
  583.  
  584.   TestDisplay
  585.